﻿using Microsoft.AspNetCore.Mvc.Routing;

using System;

namespace zijian666.WebApi.Attributes
{
    /// <summary>
    /// 标识 WebApi
    /// </summary>
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
    public class HttpApiAttribute : HttpMethodAttribute
    {
        /// <summary>
        /// 自定义路由构造函数, 默认支持方法  "GET", "POST", "DELETE", "PUT", "PATCH"
        /// </summary>
        public HttpApiAttribute()
            : this("[action]")
        {

        }
        /// <summary>
        /// 自定义路由构造函数, 默认支持方法  "GET", "POST", "DELETE", "PUT", "PATCH"
        /// </summary>
        /// <param name="template">自定义路由</param>
        public HttpApiAttribute(string? template = "[action]")
            : this(template, Array.Empty<string>())
        {
        }

        /// <summary>
        /// 构造函数,自定义路由和支持方法
        /// </summary>
        /// <param name="template">自定义路由</param>
        /// <param name="httpMethods">支持方法</param>
        public HttpApiAttribute(string? template, params string[]? httpMethods)
            : base(httpMethods is null || httpMethods.Length == 0 ? new[] { "GET", "POST", "DELETE", "PUT", "PATCH" } : httpMethods,
                  template is null || string.IsNullOrWhiteSpace(template) ? "[action]" : template.Trim())
        {
        }

        /// <summary>
        /// 构造函数,自定义支持方法
        /// </summary>
        /// <param name="httpMethods">支持方法</param>
        public HttpApiAttribute(params string[] httpMethods)
            : this(null, httpMethods)
        {
        }
    }
}
